package router

import (
	"go-bluebell/controller"
	"go-bluebell/initialize"
	"go-bluebell/utils"
	"net/http"

	"github.com/gin-gonic/gin"
)

func Setup() (r *gin.Engine) {
	r = gin.New()

	r.Use(initialize.GinLogger(), initialize.GinRecovery(true))

	// 测试判断用户是否登录的路由
	r.GET("/ping", utils.JWTAuthMiddleware(), func(c *gin.Context) {
		// 如果是你登录的用户，判断请求头中是否有有效的JWT
		c.String(http.StatusOK, "pong")
	})

	r.GET("/", func(c *gin.Context) {
		c.JSON(200, gin.H{
			"message": "Hello World!",
		})
	})

	v1 := r.Group("/api/v1")

	// 注册业务的路由
	v1.POST("/signup", controller.RegisterHandle)

	// 登录业务的路由
	v1.POST("/login", controller.LoginHandle)

	// 根据时间或分数获取帖子列表
	v1.GET("/posts2", controller.GetPostListHandler2)
	v1.GET("/posts", controller.GetPostListHandler)
	v1.GET("/community", controller.CommunityHandler)
	v1.GET("/community/:id", controller.CommunityDetailHandler)
	v1.GET("/post/:id", controller.GetPostDetailHandler)

	v1.Use(utils.JWTAuthMiddleware()) // 应用JWT认证中间件
	{
		v1.POST("/post", controller.CreatePostHandler)
		v1.POST("/vote", controller.PostVoteController)
	}

	r.NoRoute(func(c *gin.Context) {
		c.JSON(http.StatusOK, gin.H{
			"msg": "404",
		})
	})

	return
}
